<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
  <meta name="renderer" content="webkit">

  
  <title>Android 10.0 动态壁纸 LiveWallpaper | 龙之叶的博客</title>

  <link rel="shortcut icon" href="/images/favicon.png">
  <link rel="alternate" href="/atom.xml" title="龙之叶的博客" type="application/atom+xml">
  <meta name="description" content="Android 10.0 动态壁纸 LiveWallpaper">
<meta property="og:type" content="article">
<meta property="og:title" content="Android 10.0 动态壁纸 LiveWallpaper">
<meta property="og:url" content="http://longzhiye.top/2023/12/31/2023-12-31/index.html">
<meta property="og:site_name" content="龙之叶的博客">
<meta property="og:description" content="Android 10.0 动态壁纸 LiveWallpaper">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://longzhiye.top/images/20231231/20231231093328.png">
<meta property="article:published_time" content="2023-12-30T16:00:00.000Z">
<meta property="article:modified_time" content="2024-08-29T02:40:34.918Z">
<meta property="article:author" content="龙之叶">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="SystemUI">
<meta property="article:tag" content="framework">
<meta property="article:tag" content="Launcher">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://longzhiye.top/images/20231231/20231231093328.png">

  <meta name="keywords" content=",Android,SystemUI,framework,Launcher">
  <meta name="format-detection" content="telephone=no,email=no">
  <meta name="theme-color" content="#9C27B0">

  <meta name="mobile-web-app-capable" content="yes">
  <meta name="application-name" content="龙之叶的博客">
  <meta name="msapplication-starturl" content="http://longzhiye.top/2023/12/31/2023-12-31/">
  <meta name="msapplication-navbutton-color" content="#9C27B0">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-title" content="龙之叶的博客">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/images/favicon.png">

  
    <link rel="canonical" href="http://longzhiye.top/2023/12/31/2023-12-31/">
  

  
  

  
  
  

  
<link rel="stylesheet" href="/css/mdui.css">
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/custom.css">

<meta name="generator" content="Hexo 5.4.2"></head>

<body class="mdui-appbar-with-toolbar mdui-theme-primary-indigo mdui-theme-accent-pink">
  <script>var a=localStorage.getItem("mdui-theme-layout-dark");if(a){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){document.getElementsByTagName("body")[0].className+=" mdui-theme-layout-dark"};</script>
  <script>var a=localStorage.getItem("mdui-drawer-close");if(!a){document.getElementsByTagName("body")[0].className+=" mdui-drawer-body-left"};</script>
  <header id="header" class="mdui-appbar mdui-appbar-fixed mdui-appbar-scroll-hide mdui-appbar-inset">
  <div class="mdui-toolbar mdui-color-theme">
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-drawer="{target: '#sidebar', swipe: true}"><i class="mdui-icon material-icons">menu</i></a>
    <a href="/" class="mdui-typo-headline">龙之叶的博客</a>
    <div class="mdui-toolbar-spacer"></div>
    <a href="javascript:;" class="mdui-btn mdui-btn-icon" mdui-dialog="{target: '#search'}" mdui-tooltip="{content: '搜索'}"><i class="mdui-icon material-icons">search</i></a>
	<!--
    <a href="/atom.xml" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'RSS'}"><i class="mdui-icon material-icons">rss_feed</i></a>
	-->
  </div>
</header>
<div class="mdui-dialog" id="search">
  
    <div class="search-form">
      <input type="search" class="search-form-input" placeholder="请输入关键字">
    </div>
    <div class="search-result" data-resource="/search.xml"></div>
  
</div>

  <aside id="sidebar" class="mdui-drawer mdui-drawer-full-height">
  <script>var a=localStorage.getItem("mdui-drawer-close");if(a){document.getElementById("sidebar").className+=" mdui-drawer-close"};</script>
  <div class="mdui-grid-tile">
    <img src="/images/banner.png" style="height: 160px;">
    <img src="/images/avatar.png" class="avatar-animation" style="position: absolute; top: 10%; left: 24px; width: 64px; height: 64px; border: 2px solid #fff; border-radius: 50%;background-color:#fff;">
    <div class="mdui-grid-tile-actions">
      <div class="mdui-grid-tile-text">
        <div class="mdui-grid-tile-title">龙之叶</div>
        <div class="mdui-grid-tile-subtitle"><i class="mdui-icon material-icons">art_track</i>生命不息，折腾不止</div>
      </div>
      
        <div class="mdui-grid-tile-buttons">
          <a href="mailto:longzhiye163@163.com" class="mdui-btn mdui-btn-icon" mdui-tooltip="{content: 'longzhiye163@163.com'}"><i class="mdui-icon material-icons">email</i></a>
        </div>
      
    </div>
  </div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    <a href="/" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-blue">home</i>
      <div class="mdui-list-item-content">主页</div>
    </a>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-0");if(a){document.getElementsByClassName("mdui-collapse-item")[0].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-deep-orange">archive</i>
        <div class="mdui-list-item-content">归档</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/archives/2025/07/">七月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/06/">六月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/03/">三月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/02/">二月 2025<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2025/01/">一月 2025<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/10/">十月 2024<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/09/">九月 2024<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/08/">八月 2024<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/03/">三月 2024<span class="mdui-ripple sidebar_archives-count">5</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/02/">二月 2024<span class="mdui-ripple sidebar_archives-count">11</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2024/01/">一月 2024<span class="mdui-ripple sidebar_archives-count">9</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/12/">十二月 2023<span class="mdui-ripple sidebar_archives-count">10</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/11/">十一月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/10/">十月 2023<span class="mdui-ripple sidebar_archives-count">8</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/09/">九月 2023<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/08/">八月 2023<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/06/">六月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/05/">五月 2023<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/04/">四月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/03/">三月 2023<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2023/02/">二月 2023<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/09/">九月 2022<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2022/08/">八月 2022<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/04/">四月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/03/">三月 2017<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/02/">二月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2017/01/">一月 2017<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/12/">十二月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/11/">十一月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/10/">十月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/archives/2016/08/">八月 2016<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-1");if(a){document.getElementsByClassName("mdui-collapse-item")[1].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-green">class</i>
        <div class="mdui-list-item-content">分类</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术<span class="mdui-ripple sidebar_archives-count">98</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E7%94%9F%E6%B4%BB/">生活<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/categories/%E9%80%9A%E7%9F%A5/">通知<span class="mdui-ripple sidebar_archives-count">4</span></a>
        
      </div>
    </div>
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-2");if(a){document.getElementsByClassName("mdui-collapse-item")[2].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-brown">bookmark</i>
        <div class="mdui-list-item-content">标签</div>
        <i class="mdui-collapse-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
        <a class="mdui-ripple sidebar_archives-link" href="/tags/ANR/" rel="tag">ANR<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/APP/" rel="tag">APP<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android/" rel="tag">Android<span class="mdui-ripple sidebar_archives-count">89</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android12/" rel="tag">Android12<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android13/" rel="tag">Android13<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Android14/" rel="tag">Android14<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AndroidStudio/" rel="tag">AndroidStudio<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/AutoDraw/" rel="tag">AutoDraw<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Crash/" rel="tag">Crash<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Foxmail/" rel="tag">Foxmail<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Framework/" rel="tag">Framework<span class="mdui-ripple sidebar_archives-count">20</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GPS%E8%BD%A8%E8%BF%B9%E5%9B%BE/" rel="tag">GPS轨迹图<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Git/" rel="tag">Git<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/GitHub/" rel="tag">GitHub<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Google/" rel="tag">Google<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Hexo/" rel="tag">Hexo<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Java/" rel="tag">Java<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Kernel/" rel="tag">Kernel<span class="mdui-ripple sidebar_archives-count">4</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Launcher/" rel="tag">Launcher<span class="mdui-ripple sidebar_archives-count">17</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Lib/" rel="tag">Lib<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Linux/" rel="tag">Linux<span class="mdui-ripple sidebar_archives-count">18</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Markdown/" rel="tag">Markdown<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Material-Design/" rel="tag">Material Design<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OS-X/" rel="tag">OS X<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/OTA/" rel="tag">OTA<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/QuickTime/" rel="tag">QuickTime<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SSH/" rel="tag">SSH<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Settings/" rel="tag">Settings<span class="mdui-ripple sidebar_archives-count">6</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/SystemUI/" rel="tag">SystemUI<span class="mdui-ripple sidebar_archives-count">19</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Ubuntu/" rel="tag">Ubuntu<span class="mdui-ripple sidebar_archives-count">7</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WPS/" rel="tag">WPS<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Wifi/" rel="tag">Wifi<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/WindTerm/" rel="tag">WindTerm<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/Windows/" rel="tag">Windows<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/androidmk/" rel="tag">androidmk<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/blog/" rel="tag">blog<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/build/" rel="tag">build<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/feel/" rel="tag">feel<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/framework/" rel="tag">framework<span class="mdui-ripple sidebar_archives-count">50</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/git/" rel="tag">git<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/inotify/" rel="tag">inotify<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/life/" rel="tag">life<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/ninja/" rel="tag">ninja<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/rsync/" rel="tag">rsync<span class="mdui-ripple sidebar_archives-count">2</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/standard/" rel="tag">standard<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/test/" rel="tag">test<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1/" rel="tag">企业微信<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" rel="tag">操作系统<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%B5%8B%E8%AF%95/" rel="tag">测试<span class="mdui-ripple sidebar_archives-count">1</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E6%BA%90%E7%A0%81/" rel="tag">源码<span class="mdui-ripple sidebar_archives-count">3</span></a><a class="mdui-ripple sidebar_archives-link" href="/tags/%E7%94%9F%E6%B4%BB%E6%84%9F%E6%82%9F/" rel="tag">生活感悟<span class="mdui-ripple sidebar_archives-count">1</span></a>
        
      </div>
    </div>
    <a href="/about" class="mdui-list-item mdui-ripple">
      <i class="mdui-list-item-icon mdui-icon material-icons mdui-text-color-purple">person</i>
      <div class="mdui-list-item-content">关于</div>
    </a>
  </div>

  <div class="mdui-divider"></div>

  <div class="mdui-list" mdui-collapse="{accordion: true}">
    
      <a href="/timeline" class="mdui-list-item mdui-ripple">时间轴</a>
    
    <div class="mdui-collapse-item">
      <script>var a=localStorage.getItem("mdui-collapse-item-3");if(a){document.getElementsByClassName("mdui-collapse-item")[3].className+=" mdui-collapse-item-open"};</script>
      <div class="mdui-collapse-item-header mdui-list-item mdui-ripple">
        <div class="mdui-list-item-content">友情链接</div>
        <i class="mdui-list-item-icon mdui-icon material-icons">link</i>
      </div>
      <div class="mdui-collapse-item-body mdui-list mdui-list-dense">
        
          <a href="http://www.niemingzhao.top" target="_blank" class="mdui-list-item mdui-p-l-2 mdui-text-color-theme-accent mdui-ripple" style="justify-content: center;">聂明照的博客</a>
        
        
      </div>
    </div>
  </div>
</aside>

  <main id="main" class="mdui-m-t-5 fadeIn animated">
  
<link rel="stylesheet" href="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  <style>#main article .mdui-card-content .center-block{display:block!important;margin-right:auto!important;margin-left:auto!important}</style>
  <style>#main article .mdui-card-content .text-center{text-align:center!important}</style>
  <article class="mdui-card mdui-m-b-5">
    <header class="mdui-card-media">
      <img src="/images/random/material-19.png" style="max-height: 240px;">
      <div class="mdui-card-media-covered">
        <div class="mdui-card-primary">
          <div class="mdui-card-primary-title">Android 10.0 动态壁纸 LiveWallpaper</div>
          <div class="mdui-card-primary-subtitle"><i class="iconfont">&#xe697;</i> 2023-12-31 / <i class="iconfont">&#xe601;</i> 龙之叶 &nbsp;&nbsp; <span id="busuanzi_container_page_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_page_pv"></span></span></div>
        </div>
      </div>
      <div class="mdui-card-menu">
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#qrcode', align: 'right'}"><i class="mdui-icon material-icons">devices</i></button>
          <ul class="mdui-menu" id="qrcode">
            
              <li class="mdui-menu-item"><a class="mdui-text-center mdui-ripple">发送到手机</a></li>
            
            <li class="mdui-menu-item" disabled>
              
                <img src="//qr.liantu.com/api.php?w=246&m=10&text=http://longzhiye.top/2023/12/31/2023-12-31/">
              
            </li>
          </ul>
        
        
          <button class="mdui-btn mdui-btn-icon mdui-text-color-white" mdui-menu="{target: '#share_menu', align: 'right'}"><i class="mdui-icon material-icons">share</i></button>
          <ul class="mdui-menu" id="share_menu">
            <li class="mdui-menu-item">
              <a href="https://service.weibo.com/share/share.php?appkey=&title=Android 10.0 动态壁纸 LiveWallpaper&url=http://longzhiye.top/2023/12/31/2023-12-31/&pic=http://longzhiye.top/images/favicon.png&searchPic=false&style=simple" target="_blank" class="mdui-ripple">分享到 Weibo</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://twitter.com/intent/tweet?text=Android 10.0 动态壁纸 LiveWallpaper&url=http://longzhiye.top/2023/12/31/2023-12-31/&via=龙之叶" target="_blank" class="mdui-ripple">分享到 Twitter</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.facebook.com/sharer/sharer.php?u=http://longzhiye.top/2023/12/31/2023-12-31/" target="_blank" class="mdui-ripple">分享到 Facebook</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://plus.google.com/share?url=http://longzhiye.top/2023/12/31/2023-12-31/" target="_blank" class="mdui-ripple">分享到 Google+</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://www.linkedin.com/shareArticle?mini=true&url=http://longzhiye.top/2023/12/31/2023-12-31/&title=Android 10.0 动态壁纸 LiveWallpaper" target="_blank" class="mdui-ripple">分享到 LinkedIn</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://connect.qq.com/widget/shareqq/index.html?site=龙之叶的博客&title=Android 10.0 动态壁纸 LiveWallpaper&pics=http://longzhiye.top/images/favicon.png&url=http://longzhiye.top/2023/12/31/2023-12-31/" target="_blank" class="mdui-ripple">分享到 QQ</a>
            </li>
            <li class="mdui-menu-item">
              <a href="https://telegram.me/share/url?url=http://longzhiye.top/2023/12/31/2023-12-31/&text=Android 10.0 动态壁纸 LiveWallpaper" target="_blank" class="mdui-ripple">分享到 Telegram</a>
            </li>
          </ul>
        
      </div>
    </header>
    <div class="mdui-card-content mdui-typo">
      <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>在 Android 中，壁纸分为动态与静态两种，但其实两者得本质都是一样。都以一个 Service 得形式在后台运行，在一个类型为 TYPE_WALLPAPER 的窗口上绘制内容。也可以这么去理解：静态壁纸是一种特殊的动态壁纸，它仅在窗口上渲染了一张图片，而不会对用户的操作做出反应。动态壁纸不能只应用于锁屏。</p>
<h4 id="壁纸实现时涉及的几个主要的类："><a href="#壁纸实现时涉及的几个主要的类：" class="headerlink" title="壁纸实现时涉及的几个主要的类："></a>壁纸实现时涉及的几个主要的类：</h4><ul>
<li><strong>WallpaperService 及其内部类 Engine：</strong> 壁纸在 WallpaperService 这个服务中运⾏，当需要实现⾃⼰的壁纸时，继承和实现这个类，是⾸先需要做的。Engine是WallpaperService中的⼀个内部类，实现了壁纸服务窗⼝的创建以及 Surface 的维护，同时 Engine 内部类还提供了 onVisibilityChanged()，onCommand() 等回调⽅法，⽤于可见状态变化和⽤户触摸事件等。Engine 类因此也是壁纸实现的核⼼类，实现和重写其接⼝在开发中也相当重要。</li>
<li><strong>WallpaperManagerService 和 WallpaperManager：</strong> WallpaperManagerService ⽤于管理壁纸的运⾏与切换，并通过 WallpaperManager 对外界提供操作壁纸的接⼝。</li>
<li><strong>WindowMangerService：</strong> 该类⽤于计算壁纸窗⼝的 Z 序，可见性以及为壁纸窗⼝应⽤动画。</li>
</ul>
<h4 id="壁纸服务的两种启动场景："><a href="#壁纸服务的两种启动场景：" class="headerlink" title="壁纸服务的两种启动场景："></a>壁纸服务的两种启动场景：</h4><h4 id="1、重启壁纸服务启动流程："><a href="#1、重启壁纸服务启动流程：" class="headerlink" title="1、重启壁纸服务启动流程："></a>1、重启壁纸服务启动流程：</h4><p>SystemService 进程启动时，会启动各种系统服务。在该类的 startOtherServices() ⽅法中会⾸先拉起 WallpaperManagerService，通过该类，WallpaperService 后⾯才得以启动。</p>
<p><strong>frameworks/base/services/core/java/com/android/server/SystemService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (context.getResources().getBoolean(R.bool.config_enableWallpaperService)) &#123;</span><br><span class="line">    t.traceBegin(<span class="string">&quot;StartWallpaperManagerService&quot;</span>);</span><br><span class="line">    mSystemServiceManager.startService(WALLPAPER_SERVICE_CLASS);</span><br><span class="line">    t.traceEnd();</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    Slog.i(TAG, <span class="string">&quot;Wallpaper service disabled by config&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>WallpaperManagerService 启动之后回调用到 systemReady() ⽅法。</p>
<p><strong>frameworks/base/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">class</span> <span class="title class_">Lifecycle</span> <span class="keyword">extends</span> <span class="title class_">SystemService</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> IWallpaperManagerService mService;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Lifecycle</span><span class="params">(Context context)</span> &#123;</span><br><span class="line">        <span class="built_in">super</span>(context);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onStart</span><span class="params">()</span> &#123;</span><br><span class="line">               <span class="comment">// 省略部分代码......</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onBootPhase</span><span class="params">(<span class="type">int</span> phase)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (mService != <span class="literal">null</span>) &#123;</span><br><span class="line">            mService.onBootPhase(phase);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onUnlockUser</span><span class="params">(<span class="type">int</span> userHandle)</span> &#123;</span><br><span class="line">      <span class="comment">// 省略部分代码......</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onBootPhase</span><span class="params">(<span class="type">int</span> phase)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) &#123;</span><br><span class="line">        systemReady();</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) &#123;</span><br><span class="line">        switchUser(UserHandle.USER_SYSTEM, <span class="literal">null</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">void</span> <span class="title function_">systemReady</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (DEBUG) Slog.v(TAG, <span class="string">&quot;systemReady&quot;</span>);</span><br><span class="line">    initialize();</span><br><span class="line">    <span class="comment">// 省略部分代码......</span></span><br><span class="line">     <span class="keyword">try</span> &#123;</span><br><span class="line">        ActivityManager.getService().registerUserSwitchObserver(</span><br><span class="line">                <span class="keyword">new</span> <span class="title class_">UserSwitchObserver</span>() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onUserSwitching</span><span class="params">(<span class="type">int</span> newUserId, IRemoteCallback reply)</span> &#123;</span><br><span class="line">                        switchUser(newUserId, reply);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;, TAG);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">        e.rethrowAsRuntimeException();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>会通过 initialize() 方法调用 loadSettingsLocked() ⽅法加载⽤户设置过的壁纸信息，然后监听⽤户是否切换⽤户 switchUser()，当切换⽤户时，switchWallpaper() 会调⽤ bindWallpaperComponentLocked() ⽅法拉起对应的壁纸服务。</p>
<h4 id="2、⼿动切换时壁纸服务的启动流程"><a href="#2、⼿动切换时壁纸服务的启动流程" class="headerlink" title="2、⼿动切换时壁纸服务的启动流程"></a>2、⼿动切换时壁纸服务的启动流程</h4><p>⼿动切换壁纸（这里说的是动态壁纸）时需要通过WallpaperManager.getInstance(activity).setWallpaperComponent() ⽅法完成，我们在这个接⼝中传⼊壁纸服务对应的 ComponentName，getIWallpaperManager 返回的是 WallpaperManagerService 的 Bp(binder proxy binder代理) 端，在 WallpaperManagerService 端，我们可以查看到 setWallpaperComponent 的具体实现：</p>
<blockquote>
<p>注意：WallpaperManager.getIWallpaperManager() 并没有作为 SDK 的一部分提供给开发者。因此第三方应用无法进行动态壁纸的设置。</p>
</blockquote>
<p><strong>frameworks/base/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">setWallpaperComponent</span><span class="params">(ComponentName name, <span class="type">int</span> userId)</span> &#123;</span><br><span class="line">     userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId,</span><br><span class="line">             <span class="literal">false</span> <span class="comment">/* all */</span>, <span class="literal">true</span> <span class="comment">/* full */</span>, <span class="string">&quot;changing live wallpaper&quot;</span>, <span class="literal">null</span> <span class="comment">/* pkg */</span>);</span><br><span class="line">     <span class="comment">/* ⾸先调⽤该⽅法的时候回去校验权限，该权限定义在frameworks/base/core/res/AndroidManifest.xml，</span></span><br><span class="line"><span class="comment">        &lt;permission android:name=&quot;android.permission.SET_WALLPAPER_COMPONENT&quot;</span></span><br><span class="line"><span class="comment">         android:protectionLevel=&quot;signature|privileged&quot; /&gt;</span></span><br><span class="line"><span class="comment">        查看protectionLevel，只有是特权应⽤或者系统签名的应⽤才能获取到这个系统权限，所以普通的应⽤是没有办法进⾏壁纸设置的</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">     <span class="comment">// 设置动态壁纸需要调用者拥有一个签名级的系统权限</span></span><br><span class="line">     checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);</span><br><span class="line">     <span class="type">int</span> <span class="variable">which</span> <span class="operator">=</span> FLAG_SYSTEM;</span><br><span class="line">     <span class="type">boolean</span> <span class="variable">shouldNotifyColors</span> <span class="operator">=</span> <span class="literal">false</span>;</span><br><span class="line">     WallpaperData wallpaper;</span><br><span class="line">     <span class="keyword">synchronized</span> (mLock) &#123;</span><br><span class="line">         Slog.v(TAG, <span class="string">&quot;setWallpaperComponent name=&quot;</span> + name);</span><br><span class="line">         <span class="comment">/*</span></span><br><span class="line"><span class="comment">           此处会先通过当前的⽤户ID获取到与该⽤户相关的壁纸信息，WallpaperManagerService⽀持多⽤户机制，</span></span><br><span class="line"><span class="comment">           ⽤户的信息在mWallpaperMap中存储，每⼀个⽤户对应⼀个WallpaperData，</span></span><br><span class="line"><span class="comment">           WallpaperData存储壁纸相关信息，并在随后更新其内容使之保存新壁纸的信息。</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">         wallpaper = mWallpaperMap.get(userId);</span><br><span class="line">         <span class="keyword">if</span> (wallpaper == <span class="literal">null</span>) &#123;</span><br><span class="line">             <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalStateException</span>(<span class="string">&quot;Wallpaper not yet initialized for user &quot;</span> + userId);</span><br><span class="line">         &#125;</span><br><span class="line">         <span class="comment">// 省略部分代码......</span></span><br><span class="line">         <span class="comment">// New live wallpaper is also a lock wallpaper if nothing is set</span></span><br><span class="line">         <span class="keyword">if</span> (mLockWallpaperMap.get(userId) == <span class="literal">null</span>) &#123;</span><br><span class="line">             which |= FLAG_LOCK;</span><br><span class="line">         &#125;</span><br><span class="line">         <span class="keyword">try</span> &#123;</span><br><span class="line">             wallpaper.imageWallpaperPending = <span class="literal">false</span>;</span><br><span class="line">             <span class="type">boolean</span> <span class="variable">same</span> <span class="operator">=</span> changingToSame(name, wallpaper);</span><br><span class="line">             <span class="comment">//   在这⾥真正会去拉起对应的 WallPaperService</span></span><br><span class="line">             <span class="keyword">if</span> (bindWallpaperComponentLocked(name, <span class="literal">false</span>, <span class="literal">true</span>, wallpaper, <span class="literal">null</span>)) &#123;</span><br><span class="line">                 <span class="comment">// 省略部分代码......</span></span><br><span class="line">             &#125;</span><br><span class="line">         &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">             Binder.restoreCallingIdentity(ident);</span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line">       <span class="comment">// 省略部分代码......</span></span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>上述两种场景都是通过 bindWallpaperComponentLocked() 方法来拉起相关服务的。下面看看 bindWallpaperComponentLocked() 方法：<br><strong>frameworks/base/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="type">boolean</span> <span class="title function_">bindWallpaperComponentLocked</span><span class="params">(ComponentName componentName, <span class="type">boolean</span> force,</span></span><br><span class="line"><span class="params">        <span class="type">boolean</span> fromUser, WallpaperData wallpaper, IRemoteCallback reply)</span> &#123;</span><br><span class="line"> </span><br><span class="line">    <span class="comment">// 省略部分代码......</span></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="comment">// 当componentName为null时表示使用默认壁纸。</span></span><br><span class="line">        <span class="comment">// 这里会将componentName参数改为默认壁纸的componentName</span></span><br><span class="line">        <span class="keyword">if</span> (componentName == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="comment">// mDefaultWallpaperComponent是从com.android.internal.R.string.default_wallpaper_component</span></span><br><span class="line">            <span class="comment">// 中获取默认壁纸的componentName。这个值的设置位于res/values/config.xml中，</span></span><br><span class="line">            <span class="comment">// 开发者可以通过修改这个值设置默认壁纸</span></span><br><span class="line">            componentName = mDefaultWallpaperComponent;</span><br><span class="line">            <span class="comment">// 倘若在上述的资源文件中没有指定一个默认壁纸，即default_wallpaper_component的</span></span><br><span class="line">            <span class="comment">// 值被设置为@null)，则使用ImageWallpaper代替默认壁纸。ImageWallpaper就是前文所述的静态壁纸,</span></span><br><span class="line">            <span class="comment">// 路径在：R.string.image_wallpaper_component</span></span><br><span class="line">            <span class="keyword">if</span> (componentName == <span class="literal">null</span>) &#123;</span><br><span class="line">                componentName = mImageWallpaper;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 接下来WallpaperMangerService会尝试从PackageManager中尝试获取ComponentName所</span></span><br><span class="line">        <span class="comment">// 指定的Service的描述信息,获取此信息的目的在于确认该Service是一个符合要求的壁纸服务</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">serviceUserId</span> <span class="operator">=</span> wallpaper.userId;</span><br><span class="line">        <span class="type">ServiceInfo</span> <span class="variable">si</span> <span class="operator">=</span> mIPackageManager.getServiceInfo(componentName,</span><br><span class="line">                PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId);</span><br><span class="line">        <span class="keyword">if</span> (si == <span class="literal">null</span>) &#123;</span><br><span class="line">         </span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 第一个检查，要求这个Service必须声明其访问权限为 BIND_WALLPAPER。</span></span><br><span class="line">        <span class="comment">// 这个签名级的系统权限这是为了防止壁纸服务被第三方应用程序启动而产生混乱</span></span><br><span class="line">        <span class="keyword">if</span> (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) &#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">msg</span> <span class="operator">=</span> <span class="string">&quot;Selected service does not have &quot;</span></span><br><span class="line">                    + android.Manifest.permission.BIND_WALLPAPER</span><br><span class="line">                    + <span class="string">&quot;: &quot;</span> + componentName;</span><br><span class="line">            <span class="keyword">if</span> (fromUser) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">SecurityException</span>(msg);</span><br><span class="line">            &#125;</span><br><span class="line">            Slog.w(TAG, msg);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">WallpaperInfo</span> <span class="variable">wi</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="comment">// 第二个检查，要求这个Service必须可以用来处理android.service.wallpaper.WallpaperService这个Action</span></span><br><span class="line">        <span class="comment">// 其检查方式是从PackageManager中查询所有可以处理 android.service.wallpaper.WallpaperService的服务，</span></span><br><span class="line">        <span class="comment">// 然后检查即将启动的服务是否在PackageManager的查询结果之中</span></span><br><span class="line">        <span class="type">Intent</span> <span class="variable">intent</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Intent</span>(WallpaperService.SERVICE_INTERFACE);</span><br><span class="line">        <span class="keyword">if</span> (componentName != <span class="literal">null</span> &amp;&amp; !componentName.equals(mImageWallpaper)) &#123;</span><br><span class="line">            <span class="comment">// Make sure the selected service is actually a wallpaper service.</span></span><br><span class="line">            <span class="comment">// 获取所有可以处理android.service.wallpaper.WallpaperService的服务信息</span></span><br><span class="line">            List&lt;ResolveInfo&gt; ris =</span><br><span class="line">                    mIPackageManager.queryIntentServices(intent,</span><br><span class="line">                            intent.resolveTypeIfNeeded(mContext.getContentResolver()),</span><br><span class="line">                            PackageManager.GET_META_DATA, serviceUserId).getList();</span><br><span class="line">            <span class="comment">// 第二个检查，这个检查来校验服务是否声明了android.service.wallpaper.WallpaperService这个action。</span></span><br><span class="line">            <span class="comment">// 如果这个服务没有声明这个action的话那么，ris中就不会含有这个component信息。</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>; i&lt;ris.size(); i++) &#123;</span><br><span class="line">                <span class="type">ServiceInfo</span> <span class="variable">rsi</span> <span class="operator">=</span> ris.get(i).serviceInfo;</span><br><span class="line">                <span class="keyword">if</span> (rsi.name.equals(si.name) &amp;&amp;</span><br><span class="line">                        rsi.packageName.equals(si.packageName)) &#123;</span><br><span class="line">                    <span class="keyword">try</span> &#123;</span><br><span class="line">                        <span class="comment">// 第三个检查，获取名为android.service.wallpaper中的meta-data信息，</span></span><br><span class="line">                        <span class="comment">// 该meta-data信息中提供了缩略图，开发者，简单的描述等。会将这些信息转换成 WallpaperInfo</span></span><br><span class="line">                         <span class="comment">// 如果即将启动的服务位于查询结果之中，便可以确定这是一个壁纸服务。</span></span><br><span class="line">                        wi = <span class="keyword">new</span> <span class="title class_">WallpaperInfo</span>(mContext, ris.get(i));</span><br><span class="line">                    &#125; <span class="keyword">catch</span> (XmlPullParserException e) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (fromUser) &#123;</span><br><span class="line">                            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalArgumentException</span>(e);</span><br><span class="line">                        &#125;</span><br><span class="line">                        Slog.w(TAG, e);</span><br><span class="line">                        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">                    &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (fromUser) &#123;</span><br><span class="line">                            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalArgumentException</span>(e);</span><br><span class="line">                        &#125;</span><br><span class="line">                        Slog.w(TAG, e);</span><br><span class="line">                        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (wi == <span class="literal">null</span>) &#123;</span><br><span class="line">                <span class="comment">// wi为null表示即将启动的服务没有位于查询结果之中，或者没有提供必须的meta-data。</span></span><br><span class="line">                <span class="comment">// 此时返回false表示绑定失败</span></span><br><span class="line">                <span class="type">String</span> <span class="variable">msg</span> <span class="operator">=</span> <span class="string">&quot;Selected service is not a wallpaper: &quot;</span></span><br><span class="line">                        + componentName;</span><br><span class="line">                <span class="keyword">if</span> (fromUser) &#123;</span><br><span class="line">                    <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">SecurityException</span>(msg);</span><br><span class="line">                &#125;</span><br><span class="line">                Slog.w(TAG, msg);</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 当壁纸服务⽀持在ambient模式下进⾏绘制的时候，需要检查是否有 AMBIENT_WALLPAPER 权限。</span></span><br><span class="line">        <span class="keyword">if</span> (wi != <span class="literal">null</span> &amp;&amp; wi.supportsAmbientMode()) &#123;</span><br><span class="line">            <span class="keyword">final</span> <span class="type">int</span> <span class="variable">hasPrivilege</span> <span class="operator">=</span> mIPackageManager.checkPermission(</span><br><span class="line">                    android.Manifest.permission.AMBIENT_WALLPAPER, wi.getPackageName(),</span><br><span class="line">                    serviceUserId);</span><br><span class="line">                    <span class="comment">// 省略部分代码......</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// Bind the service!</span></span><br><span class="line">        <span class="keyword">if</span> (DEBUG) Slog.v(TAG, <span class="string">&quot;Binding to:&quot;</span> + componentName);</span><br><span class="line">        <span class="keyword">final</span> <span class="type">int</span> <span class="variable">componentUid</span> <span class="operator">=</span> mIPackageManager.getPackageUid(componentName.getPackageName(),</span><br><span class="line">                MATCH_DIRECT_BOOT_AUTO, wallpaper.userId);</span><br><span class="line">        <span class="comment">// 1、这里创建一个WallpaperConnection，它不仅实现 ServiceConnetion 接口用于监听 WallpaperService 之间的连接状态,</span></span><br><span class="line">        <span class="comment">// 同时还实现了 IWallpaperService.Stub，也就说它支持跨进程通信。</span></span><br><span class="line">        <span class="comment">// 在服务绑定成功后的 WallpaperConnection.ServiceConnetion()方法调用中，WallpaperConnection 的实例回发送给 WallpaperService，</span></span><br><span class="line">        <span class="comment">// 使其作为 WallpaperService 向 WallpaperManagerService 通信的桥梁。</span></span><br><span class="line">        <span class="type">WallpaperConnection</span> <span class="variable">newConn</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">WallpaperConnection</span>(wi, wallpaper, componentUid);</span><br><span class="line">        <span class="comment">// 为启动壁纸服务准备Intent</span></span><br><span class="line">        intent.setComponent(componentName);</span><br><span class="line">        intent.putExtra(Intent.EXTRA_CLIENT_LABEL,</span><br><span class="line">                com.android.internal.R.string.wallpaper_binding_label);</span><br><span class="line">        intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(</span><br><span class="line">                mContext, <span class="number">0</span>,</span><br><span class="line">                Intent.createChooser(<span class="keyword">new</span> <span class="title class_">Intent</span>(Intent.ACTION_SET_WALLPAPER),</span><br><span class="line">                        mContext.getText(com.android.internal.R.string.chooser_wallpaper)),</span><br><span class="line">                PendingIntent.FLAG_IMMUTABLE, <span class="literal">null</span>, <span class="keyword">new</span> <span class="title class_">UserHandle</span>(serviceUserId)));</span><br><span class="line">        <span class="comment">// 2、这⾥启动指定的壁纸服务，服务启动后，壁纸还没有办法进⾏显⽰，</span></span><br><span class="line">        <span class="comment">// 还需要WallpaperConnection.onServiceConnected中进⾏相应的处理</span></span><br><span class="line">        <span class="keyword">if</span> (!mContext.bindServiceAsUser(intent, newConn,</span><br><span class="line">                Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI</span><br><span class="line">                        | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE</span><br><span class="line">                        | Context.BIND_INCLUDE_CAPABILITIES,</span><br><span class="line">                <span class="keyword">new</span> <span class="title class_">UserHandle</span>(serviceUserId))) &#123;</span><br><span class="line">            <span class="type">String</span> <span class="variable">msg</span> <span class="operator">=</span> <span class="string">&quot;Unable to bind service: &quot;</span></span><br><span class="line">                    + componentName;</span><br><span class="line">            <span class="keyword">if</span> (fromUser) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalArgumentException</span>(msg);</span><br><span class="line">            &#125;</span><br><span class="line">            Slog.w(TAG, msg);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 3、新的的壁纸服务启动成功后，便通过detachWallpaperLocked()销毁旧有的壁纸服务</span></span><br><span class="line">        <span class="keyword">if</span> (wallpaper.userId == mCurrentUserId &amp;&amp; mLastWallpaper != <span class="literal">null</span></span><br><span class="line">                &amp;&amp; !wallpaper.equals(mFallbackWallpaper)) &#123;</span><br><span class="line">            detachWallpaperLocked(mLastWallpaper);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 4、将新的壁纸服务的运行信息保存到WallpaperData中</span></span><br><span class="line">        wallpaper.wallpaperComponent = componentName;</span><br><span class="line">        wallpaper.connection = newConn;</span><br><span class="line">        newConn.mReply = reply;</span><br><span class="line">   </span><br><span class="line">        <span class="keyword">if</span> (wallpaper.userId == mCurrentUserId &amp;&amp; !wallpaper.equals(mFallbackWallpaper)) &#123;</span><br><span class="line">            mLastWallpaper = wallpaper;</span><br><span class="line">        &#125;</span><br><span class="line">        updateFallbackConnection();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">        <span class="type">String</span> <span class="variable">msg</span> <span class="operator">=</span> <span class="string">&quot;Remote exception for &quot;</span> + componentName + <span class="string">&quot;\n&quot;</span> + e;</span><br><span class="line">        <span class="keyword">if</span> (fromUser) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">IllegalArgumentException</span>(msg);</span><br><span class="line">        &#125; </span><br><span class="line">        Slog.w(TAG, msg);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述代码主要做了两件事：</p>
<h4 id="1、检查拉起服务的条件"><a href="#1、检查拉起服务的条件" class="headerlink" title="1、检查拉起服务的条件"></a>1、检查拉起服务的条件</h4><ul>
<li>检查这个 Service 必须声明其访问权限为 BIND_WALLPAPER。为了防止壁纸服务被第三方应用程序启动而产生混乱。</li>
<li>检查服务是否声明了 android.service.wallpaper.WallpaperService 这个 action。如果这个服务没有声明这个 action 的话那么， ris 中就不会含有这个 component 信息。</li>
<li>检查这个 Service 在 meta-data 中有没有提供壁纸描述信息，该 meta-data 信息中提供了缩略图，开发者，简单的描述等。会将这些信息转换成 WallpaperInfo。</li>
</ul>
<h4 id="2、拉起壁纸服务"><a href="#2、拉起壁纸服务" class="headerlink" title="2、拉起壁纸服务"></a>2、拉起壁纸服务</h4><ul>
<li>创建了 WallpaperConnection 对象，由于实现了 ServiceConnection 接⼝，所以WallpaperConnection 可以⽤来监听和壁纸服务的连接状态，另外由于继承了IWallpoaperConnection.Stub 接⼝，所以 WallpaperConnection 具有了跨进程通信的能⼒。</li>
<li>启动壁纸服务：这⾥仅仅是拉起服务，和拉起普通服务的⽅式基本⼀致，拉起⽅式上则使⽤了 bindServiceAsUser，查看官⽅注解，该接口增加了校验该⽤户是否能拉起该服务，其余的⾏为和 bindService 相同。</li>
<li>保存当前 WallpaperConnection 实例，ConponentName，到 WallpaperData 中。</li>
</ul>
<p>bindWallpaperComponentLocked() 函数将壁纸服务拉了起来，但是仅仅将壁纸服务拉起来是没有办法显⽰图像的，因为启动的服务并没有窗口令牌，这样就没办法添加窗⼝。剩下的这部分显⽰的⼯作在 WallpaperConnection#onServiceConnected() ⽅法中进⾏，在该回调中同样也能拿到壁纸服务端提供的 Binder 对象。<br>WallpaperService 在被 bind(绑定 )的时候返回了一个 IWallpaperServiceWrapper 对象，从代码中可以看到，该对象中保存了 WallpaperService 实例：<br><strong>WallpaperConnection#onServiceConnected()</strong><br><strong>frameworks/base/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onServiceConnected</span><span class="params">(ComponentName name, IBinder service)</span> &#123;</span><br><span class="line">    <span class="keyword">synchronized</span> (mLock) &#123;</span><br><span class="line">        <span class="keyword">if</span> (mWallpaper.connection == <span class="built_in">this</span>) &#123;</span><br><span class="line">            <span class="comment">// 客户端 拿 WallpaperService 的 Binder</span></span><br><span class="line">            mService = IWallpaperService.Stub.asInterface(service);</span><br><span class="line">            <span class="comment">// 绑定壁纸服务。attachServiceLocked()会调用connectLocked()方法.</span></span><br><span class="line">            attachServiceLocked(<span class="built_in">this</span>, mWallpaper);</span><br><span class="line">            <span class="keyword">if</span> (!mWallpaper.equals(mFallbackWallpaper)) &#123;</span><br><span class="line">                <span class="comment">// 保存当前壁纸的运行状态到文件系统中，以便在系统重启或发生用户切换时可以恢复</span></span><br><span class="line">                saveSettingsLocked(mWallpaper.userId);</span><br><span class="line">            &#125;</span><br><span class="line">            FgThread.getHandler().removeCallbacks(mResetRunnable);</span><br><span class="line">            mContext.getMainThreadHandler().removeCallbacks(mTryToRebindRunnable);</span><br><span class="line">            <span class="keyword">if</span> (mPerformance != <span class="literal">null</span>) &#123;</span><br><span class="line">                mPerformance.notifyWallpaperChanged(name.getPackageName());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在 attachServiceLocked() 方法中会调用 connectLocked() 方法，connectLocked() 接口中调用了 IWallpaperServiceWrapper#attach() 方法传递了壁纸服务所需要的信息。<br><strong>WallpaperConnection#connectLocked()</strong><br><strong>frameworks/base/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">connectLocked</span><span class="params">(WallpaperConnection connection, WallpaperData wallpaper)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (connection.mService == <span class="literal">null</span>) &#123;</span><br><span class="line">        Slog.w(TAG, <span class="string">&quot;WallpaperService is not connected yet&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (DEBUG) Slog.v(TAG, <span class="string">&quot;Adding window token: &quot;</span> + mToken);</span><br><span class="line">    <span class="comment">// 向 WMS 申请注册一个WALLPAPER类型的窗口令牌，</span></span><br><span class="line">    <span class="comment">// 且之后被传递给 WallpaperService 用于作为后者添加窗口的通行证。</span></span><br><span class="line">    mWindowManagerInternal.addWindowToken(mToken, TYPE_WALLPAPER, mDisplayId);</span><br><span class="line">    <span class="keyword">final</span> <span class="type">DisplayData</span> <span class="variable">wpdData</span> <span class="operator">=</span> getDisplayDataOrCreate(mDisplayId);</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="comment">// 调用 IWallpaperService.attach()方法将壁纸服务创建窗口所需的信息传递过去。</span></span><br><span class="line">        connection.mService.attach(connection, mToken, TYPE_WALLPAPER, <span class="literal">false</span>,</span><br><span class="line">                wpdData.mWidth, wpdData.mHeight,</span><br><span class="line">                wpdData.mPadding, mDisplayId);</span><br><span class="line">    &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">        <span class="comment">// 未能在显示屏上添加壁纸</span></span><br><span class="line">        Slog.w(TAG, <span class="string">&quot;Failed attaching wallpaper on display&quot;</span>, e);</span><br><span class="line">        <span class="keyword">if</span> (wallpaper != <span class="literal">null</span> &amp;&amp; !wallpaper.wallpaperUpdating</span><br><span class="line">                &amp;&amp; connection.getConnectedEngineSize() == <span class="number">0</span>) &#123;</span><br><span class="line">            bindWallpaperComponentLocked(<span class="literal">null</span> <span class="comment">/* componentName */</span>, <span class="literal">false</span> <span class="comment">/* force */</span>,</span><br><span class="line">                    <span class="literal">false</span> <span class="comment">/* fromUser */</span>, wallpaper, <span class="literal">null</span> <span class="comment">/* reply */</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>attach() 方法回传了很多信息，其中 connection 为 WallpaperConnection 的实例。WallpaperConnection 之所以具有跨进程通信的能力是因为继承了IWallpaperConnection.Stub 类。</p>
<blockquote>
<p>该 Stub 对象中有一个重要的方法 attachEngine() 方法，因为 Engine 实现才是动态壁纸的核心，WallpaperService 会将创建好的 Engine 引用通过 attachEngine() 回传给 WallpaperManagerService 进行管理。</p>
</blockquote>
<p>IWallpaperServiceWrapper 继承了 IWallpaperService.Stub，并实现了该接口的两个方法 attach() 和 detach()。</p>
<p><strong>IWallpaperServiceWrapper</strong><br><strong>frameworks/base/core/java/android/service/wallpaper/WallpaperService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">IWallpaperServiceWrapper</span> <span class="keyword">extends</span> <span class="title class_">IWallpaperService</span>.Stub &#123;</span><br><span class="line">      <span class="keyword">private</span> <span class="keyword">final</span> WallpaperService mTarget;</span><br><span class="line">      <span class="keyword">private</span> IWallpaperEngineWrapper mEngineWrapper;</span><br><span class="line">      <span class="keyword">public</span> <span class="title function_">IWallpaperServiceWrapper</span><span class="params">(WallpaperService context)</span> &#123;</span><br><span class="line">          mTarget = context;</span><br><span class="line">      &#125;</span><br><span class="line">      </span><br><span class="line">      <span class="meta">@Override</span></span><br><span class="line">      <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">attach</span><span class="params">(IWallpaperConnection conn, IBinder windowToken,</span></span><br><span class="line"><span class="params">      <span class="type">int</span> windowType, <span class="type">boolean</span> isPreview, <span class="type">int</span> reqWidth, <span class="type">int</span> reqHeight, Rect padding,<span class="type">int</span> displayId)</span> &#123;</span><br><span class="line">          mEngineWrapper = <span class="keyword">new</span> <span class="title class_">IWallpaperEngineWrapper</span>(mTarget, conn, windowToken,</span><br><span class="line">                  windowType, isPreview, reqWidth, reqHeight, padding, displayId);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="meta">@Override</span></span><br><span class="line">      <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">detach</span><span class="params">()</span> &#123;</span><br><span class="line">          mEngineWrapper.detach();</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<p>在 attach() 方法中创建了一个 IWallpaperEngineWrapper 对象 mEngineWrapper 。</p>
<p><strong>IWallpaperEngineWrapper</strong><br><strong>frameworks/base/core/java/android/service/wallpaper/WallpaperService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">IWallpaperEngineWrapper</span> <span class="keyword">extends</span> <span class="title class_">IWallpaperEngine</span>.Stub</span><br><span class="line">        <span class="keyword">implements</span> <span class="title class_">HandlerCaller</span>.Callback &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> HandlerCaller mCaller;</span><br><span class="line">    <span class="keyword">final</span> IWallpaperConnection mConnection;</span><br><span class="line">    <span class="keyword">final</span> IBinder mWindowToken;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">int</span> mWindowType;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">boolean</span> mIsPreview;</span><br><span class="line">    <span class="type">boolean</span> mShownReported;</span><br><span class="line">    <span class="type">int</span> mReqWidth;</span><br><span class="line">    <span class="type">int</span> mReqHeight;</span><br><span class="line">    <span class="keyword">final</span> <span class="type">Rect</span> <span class="variable">mDisplayPadding</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Rect</span>();</span><br><span class="line">    <span class="keyword">final</span> <span class="type">int</span> mDisplayId;</span><br><span class="line">    <span class="keyword">final</span> DisplayManager mDisplayManager;</span><br><span class="line">    <span class="keyword">final</span> Display mDisplay;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="type">AtomicBoolean</span> <span class="variable">mDetached</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AtomicBoolean</span>();</span><br><span class="line">    Engine mEngine;</span><br><span class="line">    IWallpaperEngineWrapper(WallpaperService context,</span><br><span class="line">            IWallpaperConnection conn, IBinder windowToken,</span><br><span class="line">            <span class="type">int</span> windowType, <span class="type">boolean</span> isPreview, <span class="type">int</span> reqWidth, <span class="type">int</span> reqHeight, Rect padding,</span><br><span class="line">            <span class="type">int</span> displayId) &#123;</span><br><span class="line">        mCaller = <span class="keyword">new</span> <span class="title class_">HandlerCaller</span>(context, context.getMainLooper(), <span class="built_in">this</span>, <span class="literal">true</span>);</span><br><span class="line">        <span class="comment">// 省略部分代码......</span></span><br><span class="line">        <span class="comment">// 该消息用于壁纸服务引擎的创建</span></span><br><span class="line">        <span class="type">Message</span> <span class="variable">msg</span> <span class="operator">=</span> mCaller.obtainMessage(DO_ATTACH);</span><br><span class="line">        mCaller.sendMessage(msg);</span><br><span class="line">    &#125;</span><br><span class="line">     <span class="comment">// 省略部分代码......</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">executeMessage</span><span class="params">(Message message)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (mDetached.get()) &#123;</span><br><span class="line">            <span class="keyword">if</span> (mActiveEngines.contains(mEngine)) &#123;</span><br><span class="line">                doDetachEngine();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">switch</span> (message.what) &#123;</span><br><span class="line">            <span class="keyword">case</span> DO_ATTACH: &#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="comment">// 将IWallpaperEngineWapper对象传递给WallpaperConnection进行保存，</span></span><br><span class="line">                    <span class="comment">// 通过这个引用，WallpaperManagerService也可以通过它与engine进行通信</span></span><br><span class="line">                    mConnection.attachEngine(<span class="built_in">this</span>, mDisplayId);</span><br><span class="line">                &#125; <span class="keyword">catch</span> (RemoteException e) &#123;</span><br><span class="line">                    Log.w(TAG, <span class="string">&quot;Wallpaper host disappeared&quot;</span>, e);</span><br><span class="line">                    <span class="keyword">return</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// 创建一个引擎，该方法为抽象方法，需要子类根据自身实现具体的引擎</span></span><br><span class="line">                <span class="type">Engine</span> <span class="variable">engine</span> <span class="operator">=</span> onCreateEngine();</span><br><span class="line">                mEngine = engine;</span><br><span class="line">                mActiveEngines.add(engine);</span><br><span class="line">                <span class="comment">// 该方法中会完成窗口的创建、surface创建等工作。</span></span><br><span class="line">                engine.attach(<span class="built_in">this</span>);</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 省略部分代码......</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由于 mConnection.attachEngine() 方法将 IWallpaperEngineWrapper 传递给了WallpaperManagerService，因此 WallpaperManagerService 可以转发相关的请求和设置到 Engine 对象中，实现 WallpaperManagerService 到壁纸的通信。onCreateEngine() 方法执行后，引擎创建完成，之后通过 engine.attach()方法进行引擎相关的初始化。<br><strong>frameworks/base/core/java/android/service/wallpaper/WallpaperService.java</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Engine</span> &#123;</span><br><span class="line">    <span class="comment">// 省略部分代码......</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">attach</span><span class="params">(IWallpaperEngineWrapper wrapper)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (DEBUG) Log.v(TAG, <span class="string">&quot;attach: &quot;</span> + <span class="built_in">this</span> + <span class="string">&quot; wrapper=&quot;</span> + wrapper);</span><br><span class="line">        <span class="keyword">if</span> (mDestroyed) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        mIWallpaperEngine = wrapper;</span><br><span class="line">        mCaller = wrapper.mCaller;</span><br><span class="line">        mConnection = wrapper.mConnection;</span><br><span class="line">        mWindowToken = wrapper.mWindowToken;</span><br><span class="line">        mSurfaceHolder.setSizeFromLayout();</span><br><span class="line">        mInitializing = <span class="literal">true</span>;</span><br><span class="line">        <span class="comment">// 这个session用于和WMS进行通信</span></span><br><span class="line">        mSession = WindowManagerGlobal.getWindowSession();</span><br><span class="line">        <span class="comment">// mWindow是一个IWindow对象，用于接收从WMS发送过来的消息</span></span><br><span class="line">        mWindow.setSession(mSession);</span><br><span class="line">        mLayout.packageName = getPackageName();</span><br><span class="line">        mIWallpaperEngine.mDisplayManager.registerDisplayListener(mDisplayListener,</span><br><span class="line">                mCaller.getHandler());</span><br><span class="line">        mDisplay = mIWallpaperEngine.mDisplay;</span><br><span class="line">        mDisplayContext = createDisplayContext(mDisplay);</span><br><span class="line">        mDisplayState = mDisplay.getState();</span><br><span class="line">        <span class="keyword">if</span> (DEBUG) Log.v(TAG, <span class="string">&quot;onCreate(): &quot;</span> + <span class="built_in">this</span>);</span><br><span class="line">        <span class="comment">// 子类可以重写该接口，在该接口中可以修改mSurfaceHolder相关的属性，这个时候</span></span><br><span class="line">        <span class="comment">// 窗口尚未创建。设置的相关属性将在updateSurface中创建窗口时使用</span></span><br><span class="line">        onCreate(mSurfaceHolder);</span><br><span class="line">        mInitializing = <span class="literal">false</span>;</span><br><span class="line">        mReportedVisible = <span class="literal">false</span>;</span><br><span class="line">        <span class="comment">// updateSurface会进行窗口以及Surface的创建。</span></span><br><span class="line">        updateSurface(<span class="literal">false</span>, <span class="literal">false</span>, <span class="literal">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 省略部分代码......</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>attach() 方法执行的完成，标志着壁纸启动的完成，之后可以调用壁纸的 surface 显示图像。</p>
<p><img src="/images/20231231/20231231093328.png"></p>
<p>在WallpaperManagerService和WallpaperService交互的过程中，主要有下面三个跨进程通信的Binder对象：</p>
<ul>
<li>WallpaperConnection：实现在WallpaperManagerService中，并通过IWallpaperService.attach回调传递给了IWallpaperEngineWrapper，通过WallpaperConnection.attachEngine()方法，WallpaperService将IWallpaperEngineWrapper回传给了WallpaperManagerService，实现了双向的通信。</li>
<li>IWallpaperService：实现在WallpaperService中，该对象提供了attach方法，用于从WallpaperManagerService获取引擎创建时需要的WindowToken等信息。</li>
<li>IWallpaperEngineWrapper：实现在壁纸服务进程中，同时引用交给了WallpaperManagerService，该对象封装了Engine类，WallpaperManagerService对引擎相关的控制需要通过该对象提供的接口实现。</li>
</ul>

      
      <blockquote>
        
        <strong>本文链接：</strong><br><a href="http://longzhiye.top/2023/12/31/2023-12-31/">http://longzhiye.top/2023/12/31/2023-12-31/</a>
      </blockquote>
    </div>
    <footer class="mdui-card-actions">
      
        <a class="mdui-ripple article_categories-link" href="/categories/%E6%8A%80%E6%9C%AF/">技术</a>
      
      
        <a class="mdui-ripple article_tags-link" href="/tags/Android/" rel="tag">Android</a><a class="mdui-ripple article_tags-link" href="/tags/Launcher/" rel="tag">Launcher</a><a class="mdui-ripple article_tags-link" href="/tags/SystemUI/" rel="tag">SystemUI</a><a class="mdui-ripple article_tags-link" href="/tags/framework/" rel="tag">framework</a>
      
    </footer>
    
  </article>
  
<script src="//cdn.bootcdn.net/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

  <script>$("#main article .mdui-card-content img.fancybox").on("click",function(e){$.fancybox.open({src:$(this).attr("src")});});</script>


  <nav id="paginator">
    
      <a rel="prev" class="extend prev" href="/2024/01/01/2024-01-01/">
        <button aria-label="prev" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_back</i></button>
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android 10.0 锁屏壁纸 LockscreenWallpaper'}">上一篇</span>
      </a>
    
    <div class="spacer"></div>
    
      <a rel="next" class="extend next" href="/2023/12/30/2023-12-30/">
        <span class="mdui-p-x-3" mdui-tooltip="{content: 'Android 10.0 滑动解锁流程'}">下一篇</span>
        <button aria-label="next" class="mdui-btn mdui-btn-raised mdui-btn-icon mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_forward</i></button>
      </a>
    
  </nav>





</main>
  <footer id="footer" class="mdui-m-t-5 mdui-p-y-3 mdui-color-theme">
  <div class="mdui-p-y-0 mdui-text-center">
    
    
    
    
    
    
    
      <a href="http://github.com/longzhiye" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe7ab;</i></a>
    
    
    
      <a href="https://www.zhihu.com/people/long-zhi-xie-61" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe6c0;</i></a>
    
    
    
      <a href="tencent://tencent:////message/?uin=951898105&Menu=yes" target="_blank" class="mdui-btn mdui-btn-icon mdui-text-color-theme-a100"><i class="mdui-icon iconfont">&#xe651;</i></a>
    
    
  </div>
  <div class="mdui-p-y-1 mdui-text-center">
    Copyright &copy; 2022 - 2025 龙之叶<br>
    Powered by <a href="https://hexo.io/" target="_blank" class="mdui-text-color-theme-accent">Hexo</a>
    <br>
      <span id="busuanzi_container_site_pv" style="display: none;"><i class="iconfont">&#xe7fd;</i> <span id="busuanzi_value_site_pv"></span></span> &nbsp;&nbsp;
      <span id="busuanzi_container_site_uv" style="display: none;"><i class="iconfont">&#xe601;</i> <span id="busuanzi_value_site_uv"></span></span>
    
  </div>
</footer>

  <button id="gotop" class="mdui-fab mdui-fab-fixed mdui-fab-hide mdui-color-theme-accent mdui-ripple"><i class="mdui-icon material-icons">arrow_upward</i></button>
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  
<script src="/js/mdui.js"></script>
<script src="/js/script.js"></script>

  
<script src="/custom.js"></script>

</body>
</html>
